package com.drool.mediaplayer.Adapters;

import com.drool.mediaplayer.R;

import android.content.Context;
import android.database.Cursor;

import android.net.Uri;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio;
import android.provider.MediaStore.Video;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class VideoBrowserAdapter extends CursorAdapter{

	private static String TAG ="VideoBrowserAdapter";
	
	public VideoBrowserAdapter(Context context, Cursor c) {
		super(context, c);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		// TODO Auto-generated method stub
		//Log.i("adapter", "Binding ID" + cursor.getString(0) );
		Long duration;
		
		TextView nameTxt = (TextView)view.findViewById(R.id.text1);
		TextView artistTxt = (TextView)view.findViewById(R.id.text3);
		TextView durationTxt = (TextView)view.findViewById(R.id.text2); 
		TextView descTxt = (TextView)view.findViewById(R.id.text4);
		ImageView albumArt = (ImageView) view.findViewById(R.id.albumImage);
		
		
		nameTxt.setText(cursor.getString(cursor.getColumnIndex(Video.Media.DISPLAY_NAME)));
		artistTxt.setText("Artist: " + cursor.getString(cursor.getColumnIndex(Video.Media.ARTIST)));
		
		try{
			duration = Long.valueOf(cursor.getString(cursor.getColumnIndex(Video.Media.DURATION)));
		}catch(Exception e){
			Log.e(TAG, "Duration error:",e);
			duration = 0L;
		}
		
		
		int minDuration = (int) ((duration / 1000) / 60);
		
		int secDuration = (int) ((duration / 1000) % 60);
		
		durationTxt.setText(Integer.toString(minDuration) + ":" + Integer.toString(secDuration));
		
		albumArt.setImageResource(R.drawable.videodelta);
		if (cursor.getString(cursor.getColumnIndex(Video.Media.DESCRIPTION))!= null)
			descTxt.setText(cursor.getString(cursor.getColumnIndex(Video.Media.DESCRIPTION)));
		else
			descTxt.setText("Description: Unknown");
		
		Log.i(TAG,cursor.getString(cursor.getColumnIndex(Video.Media.DURATION)) + "/" + Long.toString(duration));
		
		
		
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		LayoutInflater inflater = LayoutInflater.from(context);
		View v = inflater.inflate(R.layout.layadapter, parent, false);
		bindView(v, context, cursor);
		return v;
	}
	
}
